import axios from "axios";
import NProgress from "nprogress";
import 'nprogress/nprogress.css'
import store from "@/store";

const request = axios.create({
    baseURL: `${process.env.VUE_APP_API}/api`,
    timeout: 5000
})

request.interceptors.request.use(config => {
    NProgress.start()
    store.commit('user/UPDATE_LOADING', {bol: true})
    const tempId = store.state.user.userTempId
    const token = store.state.user.token
    config.headers.userTempId = tempId
    config.headers.token = token
    return config
}, error => {
    return Promise.reject(error)
})

request.interceptors.response.use(response => {
    NProgress.done()
    store.commit('user/UPDATE_LOADING', {bol: false})
    if (response.data.code === 200) {
        NProgress.done()
        store.commit('user/UPDATE_LOADING', {bol: false})
        return response.data.data
    }
    return Promise.reject({message: response.data.message})
}, error => {
    return Promise.reject(error)
})

export default request